<?php

class Directories extends CActiveRecord
{
    public $delete;
    public $old_path;
    public $pasteOption;
    public $pasteAll;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function tableName()
	{
		return '{{directories}}';
	}

	public function rules()
	{
		return array(
			array('dir_name', 'required'),
			array('dir_name, dir_path, dir_root, dir_root_id, old_path', 'length', 'max'=>128),
			array('id, dir_name, dir_path, dir_root, dir_root_id', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'files' => array(self::HAS_MANY, 'Files', 'file_dir_id'),
		);
	}

	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
            'old_path' => 'Old Path',
			'dir_name' => 'Name',
			'dir_path' => 'Path',
			'dir_root' => 'Root',
            'dir_root_id'=>'Root id',
		);
	}

	public function search()
	{
		$criteria=new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('name', $this->dir_name,true);
		$criteria->compare('path', $this->dir_path,true);
		$criteria->compare('root', $this->dir_root,true);
        $criteria->compare('rootId', $this->dir_root_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function getUrl()
    {
        $dirId=$this->id;
        if(!isset($_SESSION['cat'])){
            $_SESSION['cat']=isset($_GET['cat']) ? $_GET['cat'] : null;
        }

        if(isset($_POST['create'])){
            return 'directories/create&id='.$dirId;
        }elseif(isset($_POST['delete'])){
            Yii::app()->file->delete(array('Directories', 'Files'));
        }elseif(isset($_POST['update'])){
            if(isset($_POST['Directories'])){
                foreach($_POST['Directories'] as $dir){
                    if($dir['delete']){
                        return 'directories/dirUpdate&id='.$dir['id'];
                    }
                }
            }
            if(isset($_POST['Files'])){
                foreach($_POST['Files'] as $file){
                    if($file['delete']){
                        return 'directories/fileUpdate&id='.$file['id'];
                    }
                }
            }
        }elseif(isset($_POST['copy'])){
            Yii::app()->file->copy();
        }elseif(isset($_POST['paste'])){
            Yii::app()->file->paste($dirId, array('Directories', 'Files'));
        }elseif(isset($_POST['add'])){
            return $this->addToCat();
        }

        return false;
    }

    public function filesBreadcrumbs()
    {
        $dirId=$this->id;
        $currentPath=$dirId!=1 ? CHtml::link(CHtml::encode('Files'), array('index', 'id'=>1)) : 'Files';
        $arrayPath=explode(DIRECTORY_SEPARATOR, str_replace($this->basePath, '', $this->path));
        foreach($arrayPath as $path){
            if($path && $path!=end($arrayPath)){
                $dir=$this->findByAttributes(array('dir_name'=>$path));
                $currentPath.=DIRECTORY_SEPARATOR.CHtml::link(CHtml::encode($path), array('index', 'id'=>$dir->id));
            }elseif($path && $path==end($arrayPath)){
                $currentPath.=DIRECTORY_SEPARATOR.$path;
            }
        }
        return $currentPath;
    }

    public function afterSave()
    {
        if(!is_dir($this->dir_path) && !mkdir($this->dir_path, 0777)){
            $this->delete();
            return false;
        }elseif($this->old_path && !rename($this->old_path, $this->path)){
            return false;
        }

        return true;
    }

    public function beforeDelete()
    {
        if(rmdir($this->path)){
            return true;
        }
        return false;
    }

    public function getBasePath()
    {
        return $this->findByPk(1)->path;
    }

    public function getChildren()
    {
        return $this->findAllByAttributes(array('dir_root_id'=>$this->id));
    }

    public function getFiles()
    {
        $file=new Files;
        return $file->findAllByAttributes(array('file_dir_id'=>$this->id));
    }

    public function getRoot()
    {
        return $this->dir_root;
    }

    public function getPath()
    {
        return $this->dir_path;
    }

    public function getName()
    {
        return $this->dir_name;
    }

    public function getRootId()
    {
        return $this->dir_root_id;
    }

    public function getSrc()
    {
        return str_replace($this->basePath, $this->findByPk(1)->name, $this->path);
    }

    protected function addToCat()
    {
        $id=isset($_SESSION['cat']) ? $_SESSION['cat'] : 1;
        $categories=new Categories;
        $cat=$categories->findByPk($id);
        $result=false;

        if(isset($_POST['Files'])){
            $files=new Files;
            foreach($_POST['Files'] as $file){
                if($file['delete'] && $cat->addImage($files->findByPk($file['id']))){
                    $result=true;
                }
            }
        }
        if(isset($_POST['Directories'])){
            foreach($_POST['Directories'] as $dir){
                if($dir['delete'] && $cat->addFolder($this->findByPk($dir['id']))){
                    $result=true;
                }
            }
        }

        unset($_SESSION['cat']);
        return $result ? 'categories/index&id='.$id : false;
    }
}